package guoanmaker.provider.business.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import guoanmaker.provider.business.dao.ProvideOrderFormDao;
import guoanmaker.provider.business.model.ProvideCouponInvoice;
import guoanmaker.provider.business.model.ProvideOrderForm;
import guoanmaker.provider.business.model.ReturnResult;
import guoanmaker.provider.business.modelnobind.MyException;
import guoanmaker.provider.business.repository.ProvideCouponInvoiceRepository;
import guoanmaker.provider.business.repository.ProvideOrderFormRepository;
import guoanmaker.provider.business.repository.ProviderBusinessRepository;

@Service
public class ProvideCouponInvoiceService {
	@Resource
	private ProvideOrderFormRepository provideOrderFormRepository;
	
	@Resource
	private ProvideCouponInvoiceRepository provideCouponInvoiceRepository;
	
	@Resource
	private ProviderBusinessRepository providerBusinessRepository;
	
	@Resource
	private ProvideOrderFormService provideOrderFormService;
	
	@Resource 
	private ProvideCouponInvoiceService provideCouponInvoiceService;
	
	@Resource
	private ProvideOrderFormDao provideOrderFormDao;
	/**
	 * 
	* @Description: 财务平台申请优惠券部分的发票 
	* @param orderids 订单id，以逗号分隔
	* @param businessid 服务商id
	* @param companyName 公司名称
	* @param phoneNum 电话 
	* @param taxpayerIdentifier 税号
	* @param bankName 开户行
	* @param bankNum 账号
	* @param address 地址
	* @param @return
	* @param @throws Exception
	* @author Yvonne_MGL 
	* @date 2018年4月11日 下午3:55:56     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional(rollbackOn=Exception.class)
	public void saveCouponInvoice(String orderids,String businessid,String companyName,String phoneNum,String taxpayerIdentifier,String bankName,String bankNum,String address)throws MyException{
		ProvideCouponInvoice couponInvoice=new ProvideCouponInvoice();
		couponInvoice.setCompanyName(companyName);
		couponInvoice.setPhoneNum(phoneNum);
		couponInvoice.setTaxpayerIdentifier(taxpayerIdentifier);
		couponInvoice.setBankName(bankName);
		couponInvoice.setBankNum(bankNum);
		couponInvoice.setAddress(address);
		couponInvoice.setApplyTime(new Date());
		couponInvoice.setState("1");
		couponInvoice.setBusinessid(businessid);
		provideCouponInvoiceRepository.save(couponInvoice);
		Double sumAmount=0.0;
		String ids[]=orderids.split(",");
		if(ids!=null&&ids.length>0){
			for (int i = 0; i < ids.length; i++) {
				if(ids[i]!=null&&!ids[i].equals("")){
					ProvideOrderForm orderForm=provideOrderFormRepository.findOne(ids[i]);
					Boolean b=provideOrderFormService.verifyOrderCoupon(orderForm, businessid);
					if(b){
						sumAmount+=orderForm.getPreferentialMoney();
						orderForm.setCouponInvoiceId(couponInvoice.getId());
					}else {
						break;
					}
				}
			}
			
		}
		couponInvoice.setAmountOfMoney(new BigDecimal(sumAmount));
		provideCouponInvoiceRepository.save(couponInvoice);
	}
	
	/**
	 * 
	* @Description: 创建优惠发票
	* @param @param companyName
	* @param @param phoneNum
	* @param @param taxpayerIdentifier
	* @param @param bankName
	* @param @param bankNum
	* @param @param address
	* @param @return
	* @author Yvonne_MGL 
	* @date 2018年4月11日 下午3:55:25     
	* @return ProvideCouponInvoice    
	* @throws 
	 */
	@Transactional
	public String createCoupleInvoice(String companyName,String phoneNum,String taxpayerIdentifier,String bankName,String bankNum,String address,String businessid){
		ProvideCouponInvoice couponInvoice=new ProvideCouponInvoice();
		couponInvoice.setCompanyName(companyName);
		couponInvoice.setPhoneNum(phoneNum);
		couponInvoice.setTaxpayerIdentifier(taxpayerIdentifier);
		couponInvoice.setBankName(bankName);
		couponInvoice.setBankNum(bankNum);
		couponInvoice.setAddress(address);
		couponInvoice.setApplyTime(new Date());
		couponInvoice.setState("1");
		couponInvoice.setBusinessid(businessid);
		provideCouponInvoiceRepository.save(couponInvoice);
		return couponInvoice.getId();
	}
	
	/**
	 * 
	* @Description: 确认开票（优惠券）
	* @param  id 发票id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2018年4月11日 下午4:13:24     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult permitCouponInvoice(String id){
		ReturnResult returnResult=new ReturnResult();
		ProvideCouponInvoice couponInvoice=provideCouponInvoiceRepository.findOne(id);
		if(couponInvoice==null){
			returnResult.setKey("error");
			returnResult.setValue("发票不存在");
		}else{
			couponInvoice.setState("2");
			couponInvoice.setBillingTime(new Date());
			provideCouponInvoiceRepository.save(couponInvoice);
			returnResult.setKey("success");
    		returnResult.setValue("确认开票成功");
		}
		return returnResult;
		
	}
	
	/**
	 * 
	* @Description: 查询优惠券发票
	* @param  id 服务商id
	* @param  startAmount 起始金额
	* @param  endAmount 结束金额
	* @param  startTime 起始时间
	* @param  endTime 结束时间
	* @param @return
	* @author Yvonne_MGL 
	* @date 2018年4月11日 下午4:37:40     
	* @return List<ProvideCouponInvoice>    
	* @throws 
	 */
	public Map getCouponInvoices(String id,Double startAmount,Double endAmount,String startTime,String endTime,String state, Integer pageNumber, Integer pageSize){
		return provideOrderFormDao.getCouponInvoices(id, startAmount, endAmount, startTime, endTime,state,pageNumber,pageSize);
	}

}
